package gota.processingResources;

import java.io.File;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import gate.Factory;
import gate.FeatureMap;
import gate.creole.SerialAnalyserController;

import org.w3c.dom.Node;

public class UnicodeTokenizerResource implements IProcessingResource {

	@Override
	public String getResourceName() {
		return "UnicodeTokenizer";
	}

	@Override
	public void init(SerialAnalyserController controller, Node node)
			throws Exception {
		XPath xpath = XPathFactory.newInstance().newXPath();
		
		org.w3c.dom.Node ruleUrlNode = (org.w3c.dom.Node) xpath.evaluate(
				"//RuleUrl", node, XPathConstants.NODE);
		String ruleUrl = ruleUrlNode.getTextContent();

		FeatureMap params = Factory.newFeatureMap();
		params
				.put(
						gate.creole.tokeniser.SimpleTokeniser.SIMP_TOK_RULES_URL_PARAMETER_NAME,
						new File(ruleUrl).toURI().toURL());
		params
				.put(
						gate.creole.tokeniser.SimpleTokeniser.SIMP_TOK_ENCODING_PARAMETER_NAME,
						"UTF-8");

		FeatureMap features = Factory.newFeatureMap();
		gate.creole.tokeniser.SimpleTokeniser simpleTokeniser = (gate.creole.tokeniser.SimpleTokeniser) Factory
				.createResource("gate.creole.tokeniser.SimpleTokeniser",
						params, features);

		controller.add(simpleTokeniser);

	}

}
